package com.javainterface.controller;

import com.javainterface.dao.GetList;
import com.javainterface.entity.UserLogin;
import com.javainterface.service.RedisDaoService;
import com.javainterface.service.XmlRead;
import net.sf.json.JSONObject;
import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Controller层
 * 负责映射请求，实现前台调用接口
 */
@Controller
public class MyController {

    @Autowired
    GetList getList;

    @Autowired
    XmlRead xmlRead;

    @Autowired
    RedisDaoService redisDaoService;

    //获取用户信息
    public UserLogin getUserInfoByUserAccounts(String accounts) {
        try {
            String backXml = getList.getUserInfoByUserAccounts(accounts);
            UserLogin userLogin = xmlRead.xmlToObject_getUserInfoByUserAccounts(backXml, accounts);
            return userLogin;
        } catch (Exception e) {
            System.out.println("未查询到用户:" + e);
        }
        return null;
    }

    //获取文件列表
    @PostMapping("/getDealList")
    @ResponseBody
    public JSONObject getDealList(String accounts, String workStatus) throws IOException, JDOMException {
        UserLogin userLogin = getUserInfoByUserAccounts(accounts);
        if (userLogin != null && !userLogin.getUserId().equals("0")) {
            System.out.println(userLogin);
            String backXml = getList.getDealList(userLogin.getUserId(), userLogin.getUserAccount(), workStatus);
            JSONObject xmlJson = xmlRead.xmlToJson_getDealList(backXml);
//            String retMsg = redisDaoService.redisSet("getDealList:" + workStatus + ":" + accounts, xmlJson.toString(), workStatus);
//            System.out.println(retMsg);
            return xmlJson;
        } else {
            Map<String, Object> ret = new HashMap<>();
            ret.put("code", 500);
            ret.put("message", "user not found...");
            JSONObject jsonObject = JSONObject.fromObject(ret);
            return jsonObject;
        }

    }

    @PostMapping("/getMobileList")
    @ResponseBody
    public JSONObject getMobileList() {
        String backXml = getList.information_getMobileList();
        JSONObject xmlJson = xmlRead.xmlToJson_getMobileList(backXml);
        return xmlJson;
    }

    //获取邮件列表
    @PostMapping("/getMailReceiveBoxList")
    @ResponseBody
    public JSONObject getMailReceiveBoxList(String accounts, String beginDate, String endDate) {
        UserLogin userLogin = getUserInfoByUserAccounts(accounts);
        if (userLogin != null && !userLogin.getUserId().equals("0")) {
            System.out.println(userLogin.toString());
            String backXml = getList.getMailReceiveBoxList(userLogin.getUserId(), userLogin.getUserAccount(), beginDate, endDate);
            JSONObject xmlJson = xmlRead.xmlToJson_getMailReceiveBoxList(backXml);
            return xmlJson;
        } else {
            Map<String, Object> ret = new HashMap<>();
            ret.put("code", 500);
            ret.put("message", "user not found...");
            JSONObject jsonObject = JSONObject.fromObject(ret);
            return jsonObject;
        }
    }

    //获取新闻栏目列表
    @PostMapping("/getAllInfoChannels")
    @ResponseBody
    public JSONObject getAllInfoChannels() {
        String backXml = getList.getAllInfoChannels();
        JSONObject xmlJson = xmlRead.xmlToJson_getAllInfoChannels(backXml);
        return xmlJson;
    }

    //获取新闻列表
    @PostMapping("/getInformationList")
    @ResponseBody
    public JSONObject getInformationList(String accounts, String status) {
        UserLogin userLogin = getUserInfoByUserAccounts(accounts);
        if (userLogin != null && !userLogin.getUserId().equals("0")) {
            System.out.println(userLogin.toString());
            String backXml = getList.getInformationList(userLogin.getUserId(), userLogin.getOrgid(), status);
            JSONObject xmlJson = xmlRead.xmlToJson_getInformationList(backXml);
            return xmlJson;
        } else {
            Map<String, Object> ret = new HashMap<>();
            ret.put("code", 500);
            ret.put("message", "user not found...");
            JSONObject jsonObject = JSONObject.fromObject(ret);
            return jsonObject;
        }
    }

    //获取会议列表
    @PostMapping("/getMeetingNoticeList")
    @ResponseBody
    public JSONObject getMeetingNoticeList(String accounts) {
        UserLogin userLogin = getUserInfoByUserAccounts(accounts);
        if (userLogin != null && !userLogin.getUserId().equals("0")) {
            System.out.println(userLogin.toString());
            String backXml = getList.getMeetingNoticeList(userLogin.getUserId(), userLogin.getOrgid());
            JSONObject xmlJson = xmlRead.mxlToJson_getMeetingNoticeList(backXml);
            return xmlJson;
        } else {
            Map<String, Object> ret = new HashMap<>();
            ret.put("code", 500);
            ret.put("message", "user not found...");
            JSONObject jsonObject = JSONObject.fromObject(ret);
            return jsonObject;
        }
    }

//    //用户信息插入数据库
//    @PostMapping("/getUserList")
//    @ResponseBody
//    public String getUserList(){
//        String backXml=getList.getUserList();
//        xmlRead.userInsert_getUserList(backXml);
//        return "success...";
//    }
}
